package com.zyzd.commontools.string;

import android.content.Context;
import android.text.TextUtils;
import android.util.Patterns;
import android.widget.Toast;

import com.zyzd.commontools.CommonTools;

import java.util.regex.Pattern;

/**
 * Created by 李宗源 on 2017/3/3.
 * <p>
 * 常用正则工具
 */

public class RegexUtil {
    /**
     * 手机号正则
     */
    public static final String REGEX_PHONE = "1(3|4|5|7|8)\\d{9}";
    /**
     * 座机正则
     */
    public static final String REGEX_FIXED_PHONE = "(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" +
            "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";

    /**
     * 邮政编码
     */
    public static final String REGEX_POST_CODE = "[1-9]\\d{5}";

    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式：验证车牌号
     */
    public static final String REGEX_CAR_NUMBER = "[\\u4e00-\\u9fa5]{1}[a-zA-Z]{1}[a-z0-9A-Z]{5}";

    /**
     * 手机号码
     *
     * @param phone
     * @return
     */
    public static boolean isMobile(CharSequence phone) {
        return !TextUtils.isEmpty(phone) && Patterns.PHONE.matcher(phone).matches();
    }

    /**
     * 区号+座机号码+分机号码
     *
     * @param fixedPhone
     * @return
     */
    public static boolean isFixedPhone(String fixedPhone) {
        return Pattern.matches(REGEX_FIXED_PHONE, fixedPhone);
    }

    /**
     * 匹配中国邮政编码
     *
     * @param postCode 邮政编码
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean isPostCode(String postCode) {
        return Pattern.matches(REGEX_POST_CODE, postCode);
    }

    /**
     * 验证是否是邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        return Pattern.matches(RegexUtil.REGEX_EMAIL, email);
    }

    public static boolean isCarNum(String carNum) {
        return Pattern.matches(RegexUtil.REGEX_CAR_NUMBER, carNum);
    }

    public static String getString(int resId) {
        return getContext().getResources().getString(resId);
    }

    public static void showToast(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    private static Context getContext() {
        return CommonTools.getContext();
    }
}
